package test
import org.scalatest.SequentialNestedSuiteExecution
import org.scalatest.BeforeAndAfterAll
import org.scalatest.FunSuite
import sun.awt.Mutex
import org.scalatest.BeforeAndAfter
import cwb.InputCommand
import cwb.CWBModules
import cwb.CoreComponent
import cwb.Equiv
import cwb.Polygraph
import agent.Agent

class EqTester extends FunSuite with BeforeAndAfterAll with BeforeAndAfter with SequentialNestedSuiteExecution {
  private val lock = new Object()
  private val mutex = new Mutex
  
  override
  def beforeAll {
    CWBModules.registerModule(CoreComponent)
    CWBModules.registerModule(Agent.component)
    CWBModules.registerCommands()
  }
  
  def asserter(weak: Boolean, input: String, expected: Boolean) = {
    def command(x: String) = if (weak) runWeakEq(x) else runStrongEq(x)
    assert(command(input) === expected, input + " did not generate " + expected)
  }
  
  def strongAssert(input: String, expected: Boolean) = asserter(false, input, expected)
  def weakAssert(input: String, expected: Boolean) = asserter(true, input, expected)
  
  def runStrongEq(input: String): Boolean = {
    Equiv.eqCommand(Polygraph.SGraph, input)
  }
  
  def runWeakEq(input: String): Boolean = {
    Equiv.eqCommand(Polygraph.WGraph, input)
  }
  
  def agentCommand(input: String) {
    Agent.agentCommand(input)
  }
  
  // Load a sample set of agents, for convenience.
  def loadBasicAgents() {
    InputCommand("\"testfiles/basicAgents.basic\"")
  }
  
  // Enforce serialisation of equivalence tests because of global state and references
  // (and Scala test not allowing an easy way to enforce serialisation..)
  
  before {
    mutex.lock()
  }
  
  after {
    mutex.unlock()
  }
  


}